//
//  ViewController.swift
//  AppStore_Demo
//
//  Created by yangyu on 2017/10/26.
//  Copyright © 2017年 YangYiYu. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    let screen_w = UIScreen.main.bounds.width
    var selectedCell: CardCell
    
    lazy var cardView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout ()
        layout.itemSize = CGSize (width: screen_w - 30, height: screen_w)
        layout.sectionInset = UIEdgeInsets (top: 10, left: 15, bottom: 10, right: 15)
        layout.scrollDirection = .vertical
        layout.headerReferenceSize = CGSize (width: screen_w, height: 60)
        
        let collection = UICollectionView (frame: self.view.frame, collectionViewLayout: layout)
        collection.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        collection.delegate = self
        collection.dataSource = self
        collection.register(CardCell.self, forCellWithReuseIdentifier: "CardCell")
        collection.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView")
        
        return collection
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        self.view.addSubview(cardView)
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    //MARK: DataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", for: indexPath)
    }
    //MARK: Delegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        selectedCell = collectionView.cellForItem(at: indexPath)! as! CardCell
        
        
        
    }
    
}

